package com.xpgk.xpsoft.role;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.xpsoft.common.XPSoftService;

public class RoleService extends XPSoftService {
	static public String checkRoleRecord(Record role) {
		if (role == null)
			return "role is null";
		String value = role.getStr("name");
		if (value == null)
			return "role name is null";
		value = role.getStr("state");
		if (value == null)
			role.set("state", "0");

		return "ok";
	}

	// 添加用户
	static public String saveRole(Record role) {
		String ret = checkRoleRecord(role);
		if (!ret.equals("ok"))
			return ret;
		role.remove("id");

		Db.save("FRM_ROLE", role);
		return "ok";
	}

	// 更新部门信息
	static public String updateRole(Record role) {
		if (role == null || role.get("id") == null)
			return "错误角色信息。";
		if (Db.update("FRM_ROLE", role)) {
			return "ok";
		}
		return "错误角色信息。";
	}

	// 更新部门管理员权限
	static public String updateRoleRights(int roleID, Integer[] rights) {

		Db.update("delete from FRM_ROLE_RIGHT where ROLEID=?", roleID);
		if (rights == null)
			return "ok";
		List<Record> recordList = new ArrayList<Record>();
		for (int i = 0; i < rights.length; i++) {
			Record record = new Record();
			record.set("ROLEID", roleID);
			record.set("RIGHTID", rights[i]);
			record.set("JYW", HashKit.md5(roleID + "userRole" + rights[i]));
			recordList.add(record);
		}
		Db.batchSave("FRM_ROLE_RIGHT", recordList, 100);
		return "ok";
	}

	// 得到部门权限对应的树列表
	static List<Kv> getRightListForTree(int roleID) {
		Record record = Db.findById("FRM_ROLE", roleID);
		if (record == null)
			return null;
		List<Record> all = null;
		if (record.getInt("DEPARTMENTID") == -1)
			all = Db.find("select * from FRM_RIGHT where HIDDEN<2 and STATE=1 order by SORT");
		else
			all = Db.find(Db.getSqlPara("xpSoftDepartment.findDeptRightList", record.getInt("DEPARTMENTID")));
		if (all == null)
			return null;
		List<Record> sub = Db.find(Db.getSqlPara("xpSoftRole.findRoleRightList", roleID));
		List<Kv> ret = new ArrayList<Kv>();
		ret.add(Kv.by("id", 0).set("name", "全部").set("open", true));
		int pID = 0;
		String preNode = "";
		for (Iterator<Record> iterator = all.iterator(); iterator.hasNext();) {
			Record r = (Record) iterator.next();
			Kv kv = Kv.by("id", r.getInt("id")).set("name", r.getStr("name")).set("open", false);
			String node = r.getStr("node");
			int id = r.getInt("id");
			if (node.length() == 2)
				kv.set("pId", 0);
			else {
				if (node.equals(preNode))
					kv.set("pId", pID);
				else {
					pID = id;
					preNode = node;
					node = node.substring(0, node.length() - 2);
					for (Iterator<Record> iterator2 = all.iterator(); iterator2.hasNext();) {
						Record r2 = (Record) iterator2.next();
						if (node.equals(r2.getStr("node"))) {
							kv.set("pId", r2.getInt("id"));
							break;
						}
					}
				}
			}
			for (Iterator<Record> iterator3 = sub.iterator(); iterator3.hasNext();) {
				Record r3 = (Record) iterator3.next();
				if (id == r3.getInt("id")) {
					kv.set("checked", true);
					break;
				}
			}
			ret.add(kv);
		}
		return ret;
	}
}
